package com.jhgsys.internal.base.service.impl;

import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.base.entity.BaseGoods;
import com.jhgsys.internal.base.mapper.BaseGoodsMapper;
import com.jhgsys.internal.base.service.IBaseGoodsService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.dict.entity.DictItem;
import com.jhgsys.internal.dict.service.IDictService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 商品表 Service实现
 *
 * @author admin
 * @date 2024-08-06 09:41:04
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class BaseGoodsServiceImpl extends ServiceImpl<BaseGoodsMapper, BaseGoods> implements IBaseGoodsService {

    @Autowired
    private IDictService dictService;

    /**
     * 查询（分页）
     * @author admin
     * @date 2024-08-06 09:41:04
     * @param request QueryRequest
     * @param baseGoods baseGoods
     * @return IPage<BaseGoods>
    */
    @Override
    public IPage<BaseGoods> findBaseGoodsPage(QueryRequest request, BaseGoods baseGoods) {
        LambdaQueryWrapper<BaseGoods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseGoods::getDeletedFlg, FebsConstant.VALID);
        if(StringUtils.isNotBlank(baseGoods.getTenantId())){
            queryWrapper.eq(BaseGoods::getTenantId,baseGoods.getTenantId());
        }
        if(StringUtils.isNotBlank(baseGoods.getGoodsName())){
            queryWrapper.like(BaseGoods::getGoodsName,baseGoods.getGoodsName());
        }
        if(StringUtils.isNotBlank(baseGoods.getStatus())){
            queryWrapper.like(BaseGoods::getStatus,baseGoods.getStatus());
        }

        Page<BaseGoods> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    @Override
    public IPage<BaseGoods> findBaseGoodsPageWithDicName(QueryRequest request, BaseGoods baseGoods) {
        IPage<BaseGoods> page = this.findBaseGoodsPage(request, baseGoods);
        List<BaseGoods> list = page.getRecords();
        if(!CollectionUtils.isEmpty(list)){
            List<DictItem> dictList = dictService.findDictByItemCodes("SAFETY_INCENTIVE_GOODS_UNIT"
                    ,list.stream().map(BaseGoods::getGoodsUnit).collect(Collectors.toList()));
            list.forEach(item->{
                item.setGoodsUnit(dictList.stream().filter(dic->dic.getItemCode().equals(item.getGoodsUnit())).collect(Collectors.toList()).get(0).getItemName());
            });
        }

        return page;
    }

    /**
     * 查询（所有）
     * @author admin
     * @date 2024-08-06 09:41:04
     * @param baseGoods baseGoods
     * @return List<BaseGoods>
     */
    @Override
    public List<BaseGoods> findBaseGoodsList(BaseGoods baseGoods) {
	    LambdaQueryWrapper<BaseGoods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseGoods::getDeletedFlg, FebsConstant.VALID);
        if(StringUtils.isNotBlank(baseGoods.getGoodsId())){
            if(baseGoods.getGoodsId().indexOf(StringPool.COMMA)>-1){
                List<String> ids = Arrays.asList(baseGoods.getGoodsId().split(StringPool.COMMA));
                queryWrapper.in(BaseGoods::getGoodsId,ids);
            }else {
                queryWrapper.eq(BaseGoods::getGoodsId,baseGoods.getGoodsId());
            }
        }
		if(StringUtils.isNotBlank(baseGoods.getTenantId())){
            queryWrapper.eq(BaseGoods::getTenantId, baseGoods.getTenantId());
        }
//        if(!baseGoods.getGoodsIdList().isEmpty()){
//            queryWrapper.in(BaseGoods::getGoodsId,baseGoods.getGoodsIdList());
//        }
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author admin
    * @date 2024-08-06 09:41:04
    * @param id
    * @return BaseGoods
    */
    @Override
    public BaseGoods findBaseGoodsById(String id){
        BaseGoods goods = this.baseMapper.selectById(id);
        if(ObjectUtils.isNotEmpty(goods)){
            List<DictItem> dictList = dictService.findDictByItemCodes("SAFETY_INCENTIVE_GOODS_UNIT"
                    , Collections.singletonList(goods.getGoodsUnit()));
            goods.setGoodsUnit(dictList.get(0).getItemName());
        }


        return goods;
    }

    /**
     * 新增
     * @author admin
     * @date 2024-08-06 09:41:04
     * @param baseGoods baseGoods
     */
    @Override
    @Transactional
    public void createBaseGoods(BaseGoods baseGoods) {
        baseGoods.setCreatedTime(new Date());
        this.save(baseGoods);
    }

    /**
     * 修改
     * @author admin
     * @date 2024-08-06 09:41:04
     * @param baseGoods baseGoods
     */
    @Override
    @Transactional
    public void updateBaseGoods(BaseGoods baseGoods) {
        baseGoods.setUpdatedTime(new Date());
        this.saveOrUpdate(baseGoods);
    }

    /**
     * 逻辑删除
     * @author admin
     * @date 2024-08-06 09:41:04
     * @param baseGoods baseGoods
     * @param baseGoodsIds baseGoodsIds
     */
    @Override
    @Transactional
    public void deleteBaseGoods(BaseGoods baseGoods,String baseGoodsIds) {
        List<String> list = Arrays.asList(baseGoodsIds.split(StringPool.COMMA));
        this.baseMapper.update(baseGoods,new LambdaQueryWrapper<BaseGoods>().in(BaseGoods::getGoodsId,list));
	}

    /**
    * 物理删除
    * @author admin
    * @date 2024-08-06 09:41:04
    * @param baseGoodsIds baseGoodsIds
    */
    @Override
    @Transactional
    public void physicallyDeleteBaseGoods(String baseGoodsIds) {
        List<String> list = Arrays.asList(baseGoodsIds.split(StringPool.COMMA));
        LambdaQueryWrapper<BaseGoods> wapper = new LambdaQueryWrapper<>();
        wapper.in(BaseGoods::getGoodsId,list);
        this.remove(wapper);
    }

    /**
     * 更新库存
     * @param baseGoods
     */
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public boolean updateInventory(BaseGoods baseGoods) {
        return this.baseMapper.updateInventory(baseGoods);
    }

    /**
     * 批量更新库存
     * @param baseGoodsList
     */
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public boolean updateBatchInventory(List<BaseGoods> baseGoodsList) {
        return this.baseMapper.updateBatchInventory(baseGoodsList);
    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public boolean updateLockInventory(BaseGoods baseGoods) {
        return this.baseMapper.updateLockInventory(baseGoods);
    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public boolean updateBatchLockInventory(List<BaseGoods> baseGoodsList) {
        return this.baseMapper.updateBatchLockInventory(baseGoodsList);
    }
}
